package com.jzg.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;


public class S3Util {

	protected final static Log log = Log.getLog(CaseUtil.class);
	
	private static final String accessKey = "5A6CL1WZ7CQ488FWGJ01";
	private static final String secretKey = "2mvQNzT4McAcXhLsoOIOWSMrXMM2yEnrLwYkF61p";
	private static final String bucketName = "cabinetvideo";
	private static final String serviceEndpoint = "86.1.51.250";
	private static final String signingRegion = "";
	private static final AmazonS3 s3;
	static {
		ClientConfiguration clientConfig = new ClientConfiguration();
		clientConfig.setProtocol(Protocol.HTTP);
		clientConfig.setSignerOverride("S3SignerType");
		s3 = AmazonS3ClientBuilder.standard()
			.withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(accessKey, secretKey)))
			.withClientConfiguration(clientConfig)
			.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(serviceEndpoint,signingRegion))
			.withPathStyleAccessEnabled(true).build();
	}
	
	public static List<S3ObjectSummary> listObject(String prefix,String startDate,String endDate) {
		List<S3ObjectSummary> res =  new ArrayList<S3ObjectSummary>();
		ObjectListing result = s3.listObjects(new ListObjectsRequest()
				.withBucketName(bucketName)
				.withPrefix(prefix));
        List<S3ObjectSummary> objects = result.getObjectSummaries();
        try {
        	for (S3ObjectSummary os : objects) {
            	String key = os.getKey();
            	String[] keyArr = StrUtil.getS3Key(key);
            	boolean isOk = true;
            	if(keyArr!=null && keyArr.length>1) {
            		String dateStr = keyArr[1];
            		if(!StrKit.isBlank(dateStr)) {
            			if(!StrKit.isBlank(startDate)) {
    	        			if(!DateUtil.dateCompare(dateStr, startDate)) {
    	        				isOk = false;
    	        			}
    	        		}
    	        		if(!StrKit.isBlank(endDate)) {
    	        			if(!DateUtil.dateCompare(endDate, dateStr)) {
    	        				isOk = false;
    	        			}
    	        		}
            		}
            	}
            	if(isOk) {
            		res.add(os);
            	}
            }
		} catch (Exception e) {
		}
		return res;
	}
	
	public static String getVideo(String key) {
		String root ="E:\\video\\";
		String path="";String path1="";
		if(key.contains(".mp4")) {
			path = key;
		}else {
			path = key+".mp4";
		}
		path1=key+"_record.mp4";
		try {
			File file = new File(root+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if(!file.exists()) {
				S3Object o = s3.getObject(bucketName, key);
				S3ObjectInputStream s3is = o.getObjectContent();
		        FileOutputStream fos = new FileOutputStream(file);
			    byte[] read_buf = new byte[1024];
			    int read_len = 0;
			    while ((read_len = s3is.read(read_buf)) > 0) {
			        fos.write(read_buf, 0, read_len);
			    }
			    s3is.close();
			    fos.close();
		    }
			VideoUtil.convertToMp4(new File(root+path), new File(root+path1));
		} catch (Exception e) {
			//e.printStackTrace();
			log.info(e.getMessage());
		}
		return path1;
	}
	
}
